home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tstring.cc < prev    next >
C/C++ Source or Header  |  1993-06-06  |  9KB  |  413 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /*
  4.  A test file for Strings
  5. */
  6.  
  7. #include <String.h>
  8. #include <stream.h>  // see note below on `dec(20)' about why this will go away
  9. #include <std.h>
  10. #include <assert.h>
  11.  
  12. // can't nicely echo assertions because they contain quotes
  13.  
  14. #define tassert(ex) {if (!(ex)) \
  15.                      { cerr << "failed assertion at " << __LINE__ << "\n"; \
  16.                        abort(); } }
  17.  
  18.   String X = "Hello";
  19.   String Y = "world";
  20.   String N = "123";
  21.   String c;
  22.   char*  s = ",";
  23.   Regex  r = "e[a-z]*o";
  24.  
  25. void decltest()
  26. {
  27.   String x;
  28.   cout << "an empty String:" << x << "\n";
  29.   assert(x.OK());
  30.   assert(x == "");
  31.  
  32.   String y = "Hello";
  33.   cout << "A string initialized to Hello:" << y << "\n";
  34.   assert(y.OK());
  35.   assert(y == "Hello");
  36.  
  37.   if (y[y.length()-1] == 'o')
  38.     y = y + '\n';
  39.   assert(y == "Hello\n");
  40.   y = "Hello";
  41.  
  42.   String a = y;
  43.   cout << "A string initialized to previous string:" << a << "\n";
  44.   assert(a.OK());
  45.   assert(a == "Hello");
  46.   assert(a == y);
  47.  
  48.   String b (a.at(1, 2));
  49.   cout << "A string initialized to previous string.at(1, 2):" << b << "\n";
  50.   assert(b.OK());
  51.   assert(b == "el");
  52.  
  53.   char ch = '@';
  54.   String z(ch); 
  55.   cout << "A string initialized to @:" << z << "\n";
  56.   assert(z.OK());
  57.   assert(z == "@");
  58.  
  59.   // XXX: `dec' is obsolete.  Since String.h includes iostream.h, and not
  60.   //      stream.h, we include stream.h in this file for the time being.  This
  61.   //      test will be rewritten to be done the "right" way, but for now, let's
  62.   //      save some time and go the easy route.
  63.   String n = dec(20);
  64.   cout << "A string initialized to dec(20):" << n << "\n";
  65.   assert(n.OK());
  66.   assert(n == "20");
  67.  
  68.   int i = atoi(n);
  69.   double f = atof(n);
  70.   cout << "n = " << n << " atoi(n) = " << i << " atof(n) = " << f << "\n";
  71.   assert(i == 20);
  72.   assert(f == 20);
  73.  
  74.   assert(X.OK());
  75.   assert(Y.OK());
  76.   assert(x.OK());
  77.   assert(y.OK());
  78.   assert(z.OK());
  79.   assert(n.OK());
  80.   assert(r.OK());
  81. }
  82.  
  83. void cattest()
  84. {
  85.   String x = X;
  86.   String y = Y;
  87.   String z = x + y;
  88.   cout << "z = x + y = " << z << "\n";
  89.   assert(x.OK());
  90.   assert(y.OK());
  91.   assert(z.OK());
  92.   assert(z == "Helloworld");
  93.  
  94.   x += y;
  95.   cout << "x += y; x = " << x << "\n";
  96.   assert(x.OK());
  97.   assert(y.OK());
  98.   assert(x == "Helloworld");
  99.  
  100.   y = Y;
  101.   x = X;
  102.   y.prepend(x);
  103.   cout << "y.prepend(x); y = " << y << "\n";
  104.   assert(y == "Helloworld");
  105.  
  106.   y = Y;
  107.   x = X;
  108.   cat(x, y, x, x);
  109.   cout << "cat(x, y, x, x); x = " << x << "\n";
  110.   assert(x == "HelloworldHello");
  111.  
  112.   y = Y;
  113.   x = X;
  114.   cat(y, x, x, x);
  115.   cout << "cat(y, x, x, x); x = " << x << "\n";
  116.   assert(x == "worldHelloHello");
  117.  
  118.   x = X;
  119.   y = Y;
  120.   z = x + s + ' ' + y.at("w") + y.after("w") + ".";
  121.   cout << "z = x + s +  + y.at(w) + y.after(w) + . = " << z << "\n";
  122.   assert(z.OK());
  123.   assert(z == "Hello, world.");
  124.  
  125. }
  126.  
  127. void comparetest()
  128. {  
  129.   String x = X;
  130.   String y = Y;
  131.   String n = N;
  132.   String z = x + y;
  133.  
  134.   assert(x != y);
  135.   assert(x == "Hello");
  136.   assert(x != z.at(0, 4));
  137.   assert (x < y);
  138.   assert(!(x >= z.at(0, 6)));
  139.   assert(x.contains("He"));
  140.   assert (z.contains(x));
  141.   assert(x.contains(r));
  142.  
  143.   assert(!(x.matches(r)));
  144.   assert(x.matches(RXalpha));
  145.   assert(!(n.matches(RXalpha)));
  146.   assert(n.matches(RXint));
  147.   assert(n.matches(RXdouble));
  148.  
  149.   assert(x.index("lo") == 3);
  150.   assert(x.index("l", 2) == 2);
  151.   assert(x.index("l", -1) == 3);
  152.   assert(x.index(r)  == 1);
  153.   assert(x.index(r, -2) == 1);
  154.  
  155.   assert(x.contains("el", 1));
  156.   assert(x.contains("el"));
  157.  
  158.   assert(common_prefix(x, "Help") == "Hel");
  159.   assert(common_suffix(x, "to") == "o");
  160.  
  161.   assert(fcompare(x, "hELlo") == 0);
  162.   assert(fcompare(x, "hElp") < 0);
  163. }
  164.  
  165. void substrtest()
  166. {
  167.   String x = X;
  168.  
  169.   char ch = x[0];
  170.   cout << "ch = x[0] = " << ch << "\n";
  171.   assert(ch == 'H');
  172.  
  173.   String z = x.at(2, 3);
  174.   cout << "z = x.at(2, 3) = " << z << "\n";
  175.   assert(z.OK());
  176.   assert(z == "llo");
  177.  
  178.   x.at(2, 2) = "r";
  179.   cout << "x.at(2, 2) = r; x = " << x << "\n";
  180.   assert(x.OK());
  181.   assert(x.at(2,2).OK());
  182.   assert(x == "Hero");
  183.  
  184.   x = X;
  185.   x.at(0, 1) = "j";
  186.   cout << "x.at(0, 1) = j; x = " << x << "\n";
  187.   assert(x.OK());
  188.   assert(x == "jello");
  189.  
  190.   x = X;
  191.   x.at("He") = "je";
  192.   cout << "x.at(He) = je; x = " << x << "\n";
  193.   assert(x.OK());
  194.   assert(x == "jello");
  195.  
  196.   x = X;
  197.   x.at("l", -1) = "i";
  198.   cout << "x.at(l, -1) = i; x = " << x << "\n";
  199.   assert(x.OK());
  200.   assert(x == "Helio");
  201.   
  202.   x = X;
  203.   z = x.at(r);
  204.   cout << "z = x.at(r) = " << z << "\n";
  205.   assert(z.OK());
  206.   assert(z == "ello");
  207.  
  208.   z = x.before("o");
  209.   cout << "z = x.before(o) = " << z << "\n";
  210.   assert(z.OK());
  211.   assert(z == "Hell");
  212.  
  213.   x.before("ll") = "Bri";
  214.   cout << "x.before(ll) = Bri; x = " << x << "\n";
  215.   assert(x.OK());
  216.   assert(x == "Brillo");
  217.  
  218.   x = X;
  219.   z = x.before(2);
  220.   cout << "z = x.before(2) = " << z << "\n";
  221.   assert(z.OK());
  222.   assert(z == "He");
  223.  
  224.   z = x.after("Hel");
  225.   cout << "z = x.after(Hel) = " << z << "\n";
  226.   assert(z.OK());
  227.   assert(z == "lo");
  228.  
  229.   x.after("Hel") = "p";  
  230.   cout << "x.after(Hel) = p; x = " << x << "\n";
  231.   assert(x.OK());
  232.   assert(x == "Help");
  233.  
  234.   x = X;
  235.   z = x.after(3);
  236.   cout << "z = x.after(3) = " << z << "\n";
  237.   assert(z.OK());
  238.   assert(z == "o");
  239.  
  240.   z = "  a bc";
  241.   z  = z.after(RXwhite);
  242.   cout << "z =   a bc; z = z.after(RXwhite); z =" << z << "\n";
  243.   assert(z.OK());
  244.   assert(z == "a bc");
  245. }
  246.  
  247.  
  248. void utiltest()
  249. {
  250.   String x = X;
  251.   int matches = x.gsub("l", "ll");
  252.   cout << "x.gsub(l, ll); x = " << x << "\n";
  253.   assert(x.OK());
  254.   assert(matches == 2);
  255.   assert(x == "Hellllo");
  256.  
  257.   x = X;
  258.   assert(x.OK());
  259.   matches = x.gsub(r, "ello should have been replaced by this string");
  260.   assert(x.OK());
  261.   cout << "x.gsub(r, ...); x = " << x << "\n";
  262.   assert(x.OK());
  263.   assert(matches == 1);
  264.   assert(x == "Hello should have been replaced by this string");
  265.  
  266.   matches = x.gsub(RXwhite, "#");
  267.   cout << "x.gsub(RXwhite, #); x = " << x << "\n";
  268.   assert(matches == 7);
  269.   assert(x.OK());
  270.  
  271.   String z = X + Y;
  272.   z.del("loworl");
  273.   cout << "z = x+y; z.del(loworl); z = " << z << "\n";
  274.   assert(z.OK());
  275.   assert(z == "Held");
  276.  
  277.   x = X;
  278.   z = reverse(x);
  279.   cout << "reverse(x) = " << z << "\n";
  280.   assert(z.OK());
  281.   assert(z == "olleH");
  282.  
  283.   x.reverse();
  284.   cout << "x.reverse() = " << x << "\n";
  285.   assert(x.OK());
  286.   assert(x == z);
  287.  
  288.   x = X;
  289.   z = upcase(x);
  290.   cout << "upcase(x) = " << z << "\n";
  291.   assert(z.OK());
  292.   assert(z == "HELLO");
  293.  
  294.   z = downcase(x);
  295.   cout << "downcase(x) = " << z << "\n";
  296.   assert(z.OK());
  297.   assert(z == "hello");
  298.  
  299.   z = capitalize(x);
  300.   cout << "capitalize(x) = " << z << "\n";
  301.   assert(z.OK());
  302.   assert(z == "Hello");
  303.   
  304.   z = replicate('*', 10);
  305.   cout << "z = replicate(*, 10) = " << z << "\n";
  306.   assert(z.OK());
  307.   assert(z == "**********");
  308.   assert(z.length() == 10);
  309. }
  310.  
  311. void splittest()
  312. {
  313.   String z = "This string\thas\nfive words";
  314.   cout << "z = " << z << "\n";
  315.   String w[10];
  316.   int nw = split(z, w, 10, RXwhite);
  317.   assert(nw == 5);
  318.   cout << "from split(z, RXwhite, w, 10), n words = " << nw << ":\n";
  319.   for (int i = 0; i < nw; ++i)
  320.   {
  321.     assert(w[i].OK());
  322.     cout << w[i] << "\n";
  323.   }
  324.   assert(w[0] == "This");
  325.   assert(w[1] == "string");
  326.   assert(w[2] == "has");
  327.   assert(w[3] == "five");
  328.   assert(w[4] == "words");
  329.   assert(w[5] == (char*)0);
  330.  
  331.   z = join(w, nw, "/");
  332.   cout << "z = join(w, nw, /); z =" << z << "\n";
  333.   assert(z.OK());
  334.   assert(z == "This/string/has/five/words");
  335. }
  336.  
  337.  
  338. void iotest()
  339. {
  340.   String z;
  341.   cout << "enter a word:";
  342.   cin >> z;
  343.   cout << "word =" << z << " ";
  344.   cout << "length = " << z.length() << "\n";
  345. }
  346.  
  347. void identitytest(String a, String b)
  348. {
  349.   String x = a;
  350.   String y = b;
  351.   x += b;
  352.   y.prepend(a);
  353.   assert((a + b) == x);
  354.   assert((a + b) == y);
  355.   assert(x == y);
  356.   assert(x.after(a) == b);
  357.   assert(x.before(b, -1) == a);
  358.   assert(x.from(a) == x);
  359.   assert(x.through(b, -1) == x);
  360.   assert(x.at(a) == a);
  361.   assert(x.at(b) == b);
  362.  
  363.   assert(reverse(x) == reverse(b) + reverse(a));
  364.   
  365.   assert((a + b + a) == (a + (b + a)));
  366.  
  367.   x.del(b, -1);
  368.   assert(x == a);
  369.  
  370.   y.before(b, -1) = b;
  371.   assert(y == (b + b));
  372.   y.at(b) = a;
  373.   assert(y == (a + b));
  374.  
  375.   x = a + reverse(a);
  376.   for (int i = 0; i < 7; ++i)
  377.   {
  378.     y = x;
  379.     x += x;
  380.     assert(x.OK());
  381.     assert(x == reverse(x));
  382.     assert(x.index(y) == 0);
  383.     assert(x.index(y, -1) == x.length() / 2);
  384.   }
  385. }
  386.  
  387. void freqtest()
  388. {
  389.   String x = "Hello World";
  390.   SubString y = x.at(0,5);
  391.   assert(x.freq('l') == 3);    // char
  392.   assert(x.freq("lo") == 1);    // char*
  393.   assert(x.freq(x) == 1);    // String
  394.   assert(x.freq(y) == 1);    // SubString
  395. }
  396.  
  397. int main()
  398. {
  399.   decltest();
  400.   cattest();
  401.   comparetest();
  402.   substrtest();
  403.   utiltest();
  404.   splittest();
  405.   freqtest();
  406.   identitytest(X, X);
  407.   identitytest(X, Y);
  408.   identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
  409.   iotest();
  410.   cout << "\nEnd of test\n";
  411.   return 0;
  412. }
  413.